package com.currency.mall.ui.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.currency.mall.R;
import com.currency.mall.event.OrderEvent;
import com.currency.mall.ui.main.order.OrderStatusFragment;
import com.mifengs.commonbase.base.BaseFragment;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/12/4.
 */

public class OrderFragment extends BaseFragment {

    @BindView(R.id.stlayout_order)
    SmartTabLayout stlayoutOrder;
    @BindView(R.id.vp_order)
    ViewPager vpOrder;
    Unbinder unbinder;
    private String[] str = new String[]{"委托订单", "持仓中", "结算订单"};
    private int type= 0;
    public OrderFragment() {

    }

    public static OrderFragment newInstance() {
        OrderFragment fragment = new OrderFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_order, container, false);
        unbinder = ButterKnife.bind(this, mView);
        StatusBarCompat.setStatusBarColor(getActivity(), ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(getActivity());
        EventBus.getDefault().register(this);
        init();
        return mView;
    }

    public void reloadOrder(int type) {
        type = 0;
        init();
    }

    private void init() {
        if(vpOrder !=null) {
            FragmentPagerItems pages = new FragmentPagerItems(getContext());
            for (String titleResId : str) {
                pages.add(FragmentPagerItem.of(titleResId, OrderStatusFragment.class));
            }
            FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getActivity().getSupportFragmentManager(), pages);
            vpOrder.setAdapter(adapter);
            vpOrder.setCurrentItem(type);
            vpOrder.setOffscreenPageLimit(2);
            stlayoutOrder.setViewPager(vpOrder);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderEvent event) {
        switch (event.action) {
            case OrderEvent.ORDER_TASK_STATU:
                if(event.type==0){
                    type = 2;
                     init();
                }
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

}
